Ext.define("core.jbpm.controller.WfProcessController", {
	extend : "Ext.app.Controller",
	mixins : {
		gridUtils : "core.utils.GridUtils",
		formUtils : "core.utils.FormUtils"
	},
	init : function() {
		var self = this;
		this.control({
			/**
			 * 挂接流程
			 */
			"processgrid button[ref=addProcess]" : {
				click : function(btn) {
					var grid = btn.up("processgrid");
					var store = grid.getStore();
					var records = grid.getSelectionModel().getSelection();
					if (records == null || records.length <= 0) {
						Ext.Msg.alert("提示", "请选择数据");
					}
					var obj = records[0];
					if (obj.get('mountStatus') == "已挂接") {
						Ext.Msg.alert("提示", "该流程已经挂接");
						return;
					}
					
					Ext.Ajax.request({
						url : APPBASE + "pc/wfProcessAction!addProcessFun.action",
						params : obj.data,
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.decode(response.responseText);
							if (resObj.success) {
								store.load();
								Ext.Msg.alert("提示", resObj.obj);
							} else {
								Ext.Msg.alert("提示", resObj.obj);
							}
						}
					});
				}
			},
			/**
			 * 取消挂接
			 */
			"processgrid button[ref=calProcess]" : {
				click : function(btn) {
					var grid = btn.up("processgrid");
					var store = grid.getStore();
					var records = grid.getSelectionModel().getSelection();
					if (!records || records.length <= 0) {
						Ext.Msg.alert("提示", "请选择数据");
						return;
					}
					var obj = records[0];
					if (obj.get('mountStatus') == "未挂接") {
						Ext.Msg.alert("提示", "该流程未挂接");
						return ;
					}
					Ext.Ajax.request({
						url : APPBASE + "pc/wfProcessAction!delProcessFun.action",
						params : obj.data,
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.decode(response.responseText);
							if (resObj.success) {
								store.load();
								Ext.Msg.alert("提示", resObj.obj);
							} else {
								Ext.Msg.alert("提示", resObj.obj);
							}
						}
					});
				}
			},
			/**
			 * 单击进卡片,查询事件和任务节点
			 */
			"processgrid" : {
				itemdblclick : function(grid, record, item, index, e, eOpts) {
					var form = grid.up("processlayout").down("processform");
					var formObj = form.getForm();
					grid = form.up("processlayout").down("processgrid");
					var records = grid.getSelectionModel().getSelection();
					var obj = records[0].data;
					//主表基本信息初始化
					self.setFormValue(formObj, obj);
					
					if (obj.id != null && obj.id != "") {
						var eventGrid = form.down("processeventgrid");
						var taskGrid = form.down("taskgrid");
						taskGrid.getStore().getProxy().extraParams = {
							ids : obj.id
						};
						eventGrid.getStore().getProxy().extraParams = {
							ids : obj.id,
							orderSql : " order by orderIndex desc",
							parentType : "ProcessFunMount"
						};
						eventGrid.getStore().load();
						taskGrid.getStore().load();
					}
					grid.hide();
					form.show();
					//流程挂接基本信息设置
					Ext.Ajax.request({
						url :APPBASE + "pc/wfProcessMountInfoAction!getInfoByBaseId.action",
						params :{baseId:obj.id},
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj=Ext.JSON.decode(response.responseText);
							if(resObj.success){
								var responObj=resObj.obj;
								var form = grid.up("processlayout").down("processMountInForm");
								var formObj = form.getForm();
								//流程挂接基本信息设置
								self.setFormValue(formObj, responObj);
								//其他信息初始化
								var processFunMount=responObj['processFunMount'];
								if(processFunMount!=null && processFunMount !=''){
								   formObj.findField('processFunMountId').setValue(processFunMount.id);
								}
								
								
								var wFSystem=responObj['system'];
								if(wFSystem!=null && wFSystem !=''){
								   formObj.findField('wFSystemId').setValue(wFSystem.id);
								   formObj.findField('wFSystemName').setValue(wFSystem.name);
								}
								
								var wFModule=responObj['module'];
								if(wFModule!=null && wFModule !=''){
								   formObj.findField('wFModuleId').setValue(wFModule.id);
								   formObj.findField('wFModuleName').setValue(wFModule.name);
								}
							}
						}
					});
				}
			},
			/**
			 * 卡片返回列表
			 */
			"processform button[ref=return]" : {
				click : function(btn) {
					var form = btn.up("processform");
					var grid = form.up("processlayout").down("processgrid");
					form.hide();
					grid.show();
				}
			},
			//添加流程事件
			"processeventgrid button[ref=addEvent]" : {
				click : function(btn) {
					var grid = btn.up("processeventgrid");
					var form = grid.up("processform").getForm();
					var idValue = form.findField("id").getValue();
					self.doInsert(grid, {
								orderIndex : 0,
								eventScope:"PROCESS"
							}, {
								orderIndex : 0,
								foreignKey : idValue,
								parentType : "ProcessFunMount",
								eventScope:"PROCESS"
							}, APPBASE + "pc/wfTaskEventAction", "id");
				}
			},
			//保存流程事件
			"processeventgrid button[ref=saveEvent]" : {
				click : function(btn) {
					var grid = btn.up("processeventgrid");
					self.doSave(grid, "id", "TaskEventInfo",
							APPBASE + "pc/wfTaskEventAction");
				}
			},
			//删除流程事件
			"processeventgrid button[ref=delEvnet]" : {
				click : function(btn) {
					var grid = btn.up("processeventgrid");
					self.doRemove(grid, "id", APPBASE + "pc/wfTaskEventAction");
				}
			},
			//任务节点事件添加
			"taskeventgrid button[ref=addEvent]" : {
				click : function(btn) {
					var grid = btn.up("taskeventgrid");
					var taskGrid = btn.up("tasklayout").down("taskgrid");
					var rescords=taskGrid.getSelectionModel().getSelection();
					if(!rescords || rescords.length<=0){
						Ext.Msg.alert("提示","请选择节点数据");
						return;
					}
					var obj = rescords[0].data;
					self.doInsert(grid, {
								orderIndex : 0,
								eventScope:"TASK"
							}, {
								orderIndex : 0,
								foreignKey : obj.id,
								parentType : "TaskFuncMount",
								eventScope:"TASK"
							}, APPBASE + "pc/wfTaskEventAction", "id");
				}
			},
			//任务节点事件保存
			"taskeventgrid button[ref=saveEvent]" : {
				click : function(btn) {
					var grid = btn.up("taskeventgrid");
					self.doSave(grid, "id", "TaskEventInfo",
							APPBASE + "pc/wfTaskEventAction");
				}
			},
			//任务节点事件删除
			"taskeventgrid button[ref=delEvnet]" : {
				click : function(btn) {
					var grid = btn.up("taskeventgrid");
					self.doRemove(grid, "id", APPBASE + "pc/wfTaskEventAction");
				}
			},
			//保存任务节点信息
			"taskgrid button[ref=saveNode]" : {
				click : function(btn) {
					var grid = btn.up("taskgrid");
					self.doSave(grid, "id", "TaskFuncMount",
							APPBASE + "pc/wfTaskAction");
				}
			},
			/**
			 * 任务节点点击  load部署任务的信息。。  load当前任务的事件
			 */
			"taskgrid" : {
				itemclick : function(grid, record, item, index, e, eOpts) {
					var records = grid.getSelectionModel().getSelection();
					var obj = records[0].data;
					var id = obj.id;
				
					//读取任务事件
					var grid=grid.up("tasklayout").down("taskeventgrid");
					if (id && id != "") {
						var taskEventGrid = grid.down("taskeventgrid");
						grid.getStore().getProxy().extraParams = {
							ids : id,
							orderSql : " order by orderIndex desc",
							parentType : "TaskFuncMount"
						};
					}
					grid.getStore().load();
					//部署对象form信息
					Ext.Ajax.request({
						url : APPBASE + "pc/wfTaskAction!findWfDeployTask.action",
						params : {
							processDefinitionId : obj.processDefinitionId,
							taskNodeName : obj.taskNodeName
						},
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.decode(response.responseText);
							if (resObj.success) {
								if (resObj.obj && resObj.obj != "") {
									var formObj = grid.ownerCt.down("taskform").getForm();
									formObj.reset();
									self.setFormValue(formObj, resObj.obj);
								}
							}
						}
					});
				}
			},
			//任务表单保存
			"taskform button[ref=saveInfo]":{
				click:function(btn){
					var form=btn.up("taskform");
					var obj=self.getFormObj(form.getForm());
					var datas=new Array();
					datas.push(obj);
					var strData=self.getExcuteSql(datas,"WfDeployTask","id");
					Ext.Ajax.request({
						url : APPBASE + "pc/wfDeployTaskAction!doListUpdate.action",
						params : {strData:strData},
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj=Ext.JSON.decode(response.responseText);
							if(resObj.success){
								Ext.Msg.alert("提示","保存成功");
							}else{
								Ext.Msg.alert("提示","保存失败");
							}
						}
					});
				}
			},
			//机构选择
			"orgsWindow panel[ref=orgsPanl] button[ref=orgsChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("taskformId");
					var formObj=form.getForm();
					//单位选择处理
					var tree=Ext.getCmp("wfSetOrgsTreePanle");
					var records = tree.getSelectionModel().getSelection();
					if(null==records || undefined==records){
						Ext.Msg.alert("提示","请选择单位!");
						return;
					}
					if(records.length<0){
						Ext.Msg.alert("提示","请选择单位!");
						return;
					}
					//机构id
					var orgId=records[0].data.id;
					//机构名称
					var orgName=records[0].data.text;
					formObj.findField('orgs').setValue(orgId);
					formObj.findField('orgsName').setValue(orgName);
					Ext.getCmp("orgsTaskWindow").close();
				}
			},
			//取消
			"orgsWindow panel[ref=orgsPanl] button[ref=orgsCoseWin]":{
				click:function(btn){
					Ext.getCmp("orgsTaskWindow").close();
				}
			},
			//角色选择选择
			"roleWindow panel[ref=rolesPanl] button[ref=rolesChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("taskformId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("rolsGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('roles').setValue(obj.get('id'));
					formObj.findField('rolesName').setValue(obj.get('roleName'));
					Ext.getCmp("roleChangeWindow").close();
				}
			},
			//取消
			"roleWindow panel[ref=rolesPanl] button[ref=rolesCoseWin]":{
				click:function(btn){
					Ext.getCmp("roleChangeWindow").close();
				}
			},
			//用户选择选择
			"assigneesWindow panel[ref=userPanl] button[ref=userChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("taskformId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("assigneesGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('assignees').setValue(obj.get('id'));
					formObj.findField('assigneesName').setValue(obj.get('cnName'));
					Ext.getCmp("assigneesWindow").close();
				}
			},
			//取消
			"assigneesWindow panel[ref=userPanl] button[ref=userCoseWin]":{
				click:function(btn){
					Ext.getCmp("assigneesWindow").close();
				}
			},
			//任务方式选择
			"taskform combobox[ref=assigneTypeChange]":{
				select:function(combo,record,index){
					var form=combo.up("taskform");
					var formObj=form.getForm();
					var tVa=combo.getValue();
					var obj= new Array();
					if('ORG'==tVa){//机构
						//人为空
						obj.push('assignees');
						obj.push('assigneesName');
						self.reseetFormObj(formObj,obj);
						
					}else if('ROLE'==tVa){//角色
						//人与机构为空
						obj.push('assignees');
						obj.push('assigneesName');
						obj.push('orgs');
						obj.push('orgsName');
						self.reseetFormObj(formObj,obj);
					}else if('ASSIGNE'==tVa){//委托人
						//机构与角色空
						obj.push('roles');
						obj.push('rolesName');
						obj.push('orgs');
						obj.push('orgsName');
						self.reseetFormObj(formObj,obj);
					}
				}
			},
			//系统选择选择
			"wfSystemWindow panel[ref=wfSystemPanl] button[ref=wfSystemChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("processMountInSaveId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("wfSystemGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('wFSystemId').setValue(obj.get('id'));
					formObj.findField('wFSystemName').setValue(obj.get('name'));
					self.closeWin('wfSystemWindow');
				}
			},
			//取消
			"wfSystemWindow panel[ref=wfSystemPanl] button[ref=wfSystemCoseWin]":{
				click:function(btn){
					self.closeWin('wfSystemWindow');
				}
			},
			//项目选择选择
			"projectWindow panel[ref=projectPanl] button[ref=projectChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("processMountInSaveId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("projectGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('projectId').setValue(obj.get('id'));
					formObj.findField('projectName').setValue(obj.get('name'));
					self.closeWin('projectWindow');
				}
			},
			//取消
			"projectWindow panel[ref=projectPanl] button[ref=projectCoseWin]":{
				click:function(btn){
					self.closeWin('projectWindow');
				}
			},
			//标段选择选择
			"sectionWindow panel[ref=sectionPanl] button[ref=sectionChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("processMountInSaveId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("sectionGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('sectionId').setValue(obj.get('id'));
					formObj.findField('sectionName').setValue(obj.get('name'));
					self.closeWin('sectionWindow');
				}
			},
			//取消
			"sectionWindow panel[ref=sectionPanl] button[ref=sectionCoseWin]":{
				click:function(btn){
					self.closeWin('sectionWindow');
				}
			},
			//业务模块选择选择
			"wfModuleWindow panel[ref=wfModulePanl] button[ref=wfModuleChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("processMountInSaveId");
					var formObj=form.getForm();
					//单位选择处理
					var grid =Ext.getCmp("wfModuleGrid");
					var se=grid.getSelectionModel();
					if (se == null) {
						Ext.Msg.alert("提示", "请选择数据");
						return false;
					}
					var records = se.getSelection();
					var obj = records[0];
					
					formObj.findField('wFModuleId').setValue(obj.get('id'));
					formObj.findField('wFModuleName').setValue(obj.get('name'));
					self.closeWin('wfModuleWindow');
				}
			},
			//取消
			"wfModuleWindow panel[ref=wfModulePanl] button[ref=wfModuleCoseWin]":{
				click:function(btn){
					self.closeWin('wfModuleWindow');
				}
			},
			//机构选择
			"wfSetOrgsWindow panel[ref=wfSetOrgsPanl] button[ref=wfSetOrgsChangeWin]":{
				click:function(btn){
					var form=Ext.getCmp("processMountInSaveId");
					var formObj=form.getForm();
					//单位选择处理
					var tree=Ext.getCmp("wfSetOrgsTreePanle");
					var records = tree.getSelectionModel().getSelection();
					if(null==records || undefined==records){
						Ext.Msg.alert("提示","请选择单位!");
						return;
					}
					if(records.length<0){
						Ext.Msg.alert("提示","请选择单位!");
						return;
					}
					//机构id
					var orgId=records[0].data.id;
					//机构名称
					var orgName=records[0].data.text;
					formObj.findField('orgId').setValue(orgId);
					formObj.findField('wfSetOrgsName').setValue(orgName);
					self.closeWin('wfSetOrgsWindow');
				}
			},
			//取消
			"wfSetOrgsWindow panel[ref=wfSetOrgsPanl] button[ref=wfSetOrgsCoseWin]":{
				click:function(btn){
					self.closeWin('wfSetOrgsWindow');
				}
			},
			//挂件信息保存
			"processMountInForm button[ref=processMountInSave]":{
				click:function(btn){
					var form=btn.up("processMountInForm");
					var formObj=form.getForm();
					var proMId=formObj.findField('processFunMountId').getValue();
					if(proMId==undefined || proMId==null ||''==proMId){
						var processformId=Ext.getCmp("processId").getValue();
						formObj.findField('processFunMountId').setValue(processformId);
					}
					var obj=self.getFormObj(formObj);
					
					//url
					var url='';
					var baseId=formObj.findField('baseId').getValue();
					if(baseId==undefined || baseId==null ||''==baseId){
						url=APPBASE + "pc/wfProcessMountInfoAction!doSaveNotBaseEntity.action";
					}else{
						url=APPBASE + "pc/wfProcessMountInfoAction!doUpdateNotBaseEntity.action";
					}
					Ext.Ajax.request({
						url :url,
						params :obj,
						method : "POST",
						timeout : 4000,
						success : function(response, opts) {
							var resObj=Ext.JSON.decode(response.responseText);
							if(resObj.success){
								Ext.Msg.alert("提示","操作成功");
							}else{
								Ext.Msg.alert("提示","保存失败");
							}
						}
					});
				}
			},//流程类型选择
			"processMountInForm combobox[ref=wfSetTypeChange]":{
				select:function(combo,record,index){
					var form=combo.up("processMountInForm");
					var formObj=form.getForm();
					var tVa=combo.getValue();
					var obj= new Array();
					if('1'==tVa){//业务模块
						//项目、标段、单位为空
						obj.push('projectId');
						obj.push('sectionId');
						obj.push('orgId');
						obj.push('projectName');
						obj.push('sectionName');
						obj.push('orgName');
						self.reseetFormObj(formObj,obj);
						
					}else if('2'==tVa){//项目+业务模块
						//标段、单位为空
						obj.push('sectionId');
						obj.push('orgId');
						obj.push('sectionName');
						obj.push('orgName');
						self.reseetFormObj(formObj,obj);
						self.reseetFormObj(formObj,obj);
					}else if('3'==tVa){//标段+业务模块
						obj.push('orgId');
						obj.push('orgName');
						self.reseetFormObj(formObj,obj);
					}else if('4'==tVa){//单位+业务模块
						//项目、标段、单位为空
						obj.push('projectId');
						obj.push('sectionId');
						obj.push('projectName');
						obj.push('sectionName');
						self.reseetFormObj(formObj,obj);
					}
				}
			}
		});
	},
	views : ["core.jbpm.view.ProcessGrid", 
	        "core.jbpm.view.ProcessLayout",
			"core.jbpm.view.ProcessEventGrid", 
			"core.jbpm.view.ProcessForm",
			"core.jbpm.view.ProcessTab", 
			"core.jbpm.view.TaskEventGrid",
			"core.jbpm.view.TaskGrid", 
			"core.jbpm.view.TaskTab",
			"core.jbpm.view.TaskLayout",
			"core.jbpm.view.taskForm.AssigneTypeCombos",
			"core.jbpm.view.taskForm.OrgsButtons",
			"core.jbpm.view.taskForm.AssigneesButtons",
			"core.jbpm.view.taskForm.RolsButtons",
			"core.jbpm.view.taskForm.OrgsWindow",
			"core.jbpm.view.taskForm.RoleWindow",
			"core.jbpm.view.taskForm.RolsGrid",
			"core.jbpm.view.taskForm.AssigneesGrid",
			"core.jbpm.view.taskForm.AssigneesWindow",
			"core.jbpm.view.ProcessMountInForm",
			"core.jbpm.view.ProcessMountInfo",
			"core.jbpm.view.taskForm.ProjectButtons",
			"core.jbpm.view.taskForm.ProjectGrid",
			"core.jbpm.view.taskForm.ProjectWindow",
			"core.jbpm.view.taskForm.SectionButtons",
			"core.jbpm.view.taskForm.SectionGrid",
			"core.jbpm.view.taskForm.SectionWindow",
			"core.jbpm.view.taskForm.WfModuleButtons",
			"core.jbpm.view.taskForm.WfModuleGrid",
			"core.jbpm.view.taskForm.WfModuleWindow",
			"core.jbpm.view.taskForm.WfSetOrgsButtons",
			"core.jbpm.view.taskForm.WfSetOrgsTree",
			"core.jbpm.view.taskForm.WfSetOrgsWindow",
			"core.jbpm.view.taskForm.WfSetTypeCombos",
			"core.jbpm.view.taskForm.WfSystemButtons",
			"core.jbpm.view.taskForm.WfSystemGrid",
			"core.jbpm.view.taskForm.WfSystemWindow",
			"core.jbpm.view.OrgTreeView",
			"core.jbpm.view.TaskForm"],
	stores : ["core.jbpm.store.ProcessStore",
			"core.jbpm.store.ProcessEventStore",
			"core.jbpm.store.TaskEventStore", 
			"core.jbpm.store.AssigneTypeCombos", 
			"core.jbpm.store.RoleStore", 
			"core.jbpm.store.UserStore", 
			"core.jbpm.store.ProjectStore",
			"core.jbpm.store.SectionStore", 
			"core.jbpm.store.WfModuleStore", 
			"core.jbpm.store.WfSetOrgsStore", 
			"core.jbpm.store.WfSetTypeCombos", 
			"core.jbpm.store.WfSystemStore", 
			"core.jbpm.store.OrgTreeStore", 
			"core.jbpm.store.TaskStore"],
	models : ["core.jbpm.model.ProcessModel",
	         "core.jbpm.model.TaskEventModel",
	         "core.jbpm.model.AssigneType",
	          "core.jbpm.model.OrgsModel",
	          "core.jbpm.model.RoleModel",
	          "core.jbpm.model.UserModel",
	           "core.jbpm.model.ProjectModel",
	          "core.jbpm.model.SectionModel",
	         "core.jbpm.model.WfModuleModel",
	          "core.jbpm.model.WfSetOrgsModel",
	          "core.jbpm.model.WfSetTypeModel",
	          "core.jbpm.model.WfSystemModel",
			 "core.jbpm.model.TaskModel"]
});